package elitech.vietnam.myfashion.extend;

import com.google.android.gms.internal.cn;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import elitech.vietnam.myfashion.DownloadImageTask;
import elitech.vietnam.myfashion.ImageViewActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.entites.ProductDetail;
import elitech.vietnam.myfashion.view.RecommendGroupView;
import elitech.vietnam.myfashion.widgets.ScaleImageView;
import elitech.vietnam.myfashion.widgets.ScaleImageView.ScaleImageViewEvent;

public class DetailAdapter extends ArrayAdapter<ProductDetail> {
	private MainActivity mContext;
//	private HeaderDetailView mHeader;
	public RecommendGroupView mRecommendGroupView;
	public boolean isRecommend = true;
	
	public DetailAdapter(MainActivity context, int resource,
			int textViewResourceId, Product product) {
		super(context, resource, textViewResourceId, product.DetailList);
		this.mContext = context;
	}
	
	static class ViewHolder {
		ScaleImageView image;
		TextView txtContent;
	}
//	
//	@Override
//	public int getCount() {
//		return super.getCount()+1;
//	}
//	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
 
//		if(position==getCount()-1){
//			if(!isRecommend) return new View(mContext);
//			if(mRecommendGroupView == null) mRecommendGroupView = new RecommendGroupView(mContext, mProduct);
//			return mRecommendGroupView;
//		} else{
//			if(position == getCount()) return new View(mContext);
			View row = convertView;
			final ProductDetail detail = getItem(position);
			final ViewHolder holder;

			if (row == null) {
				
				LayoutInflater inflater = mContext.getLayoutInflater();
				row = inflater.inflate(R.layout.photodetail, parent, false);
				holder = new ViewHolder();
				
				holder.image = (ScaleImageView) row
						.findViewById(R.id.detail_imgDetail);
				holder.txtContent = (TextView) row
						.findViewById(R.id.detail_txtDetailContent);

//				holder.image.SetAnimation(true);
				holder.image.setOnDetachedFromWindow(new ScaleImageViewEvent() {
					@Override
					public void onDetachedFromWindow() {
						Log.w("Shopping", "ScaleImageView onDetachedFromWindow");
						Drawable drawable = holder.image.getDrawable();
						if (drawable != null) {
							Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
							if (bm != null && !bm.isRecycled()) {
								bm.recycle();
								bm = null;
								drawable = null;
							}
						}
					}
				});
				row.setTag(holder);
			} else 
				holder = (ViewHolder) row.getTag();
//				holder.image.setImageResource(R.drawable.loading300);
			String str = Const.SERVER_IMAGE_URL + detail.Image;
			if (str.indexOf("post") != -1)
				str = str.replace("app/", "");
			if (str.indexOf("account") != -1)
				str = str.replace("app/", "");
//			new DownloadImageTask(mContext, holder.image, false, 2).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, detail.Image);
			UrlImageViewHelper.setUrlDrawable(holder.image, str);
				
			if(detail.Content!=null && !detail.Content.equals("")){
				holder.txtContent.setText(detail.Content.replace("<br>", "\n"));
				holder.txtContent.setPadding(8, 8, 8, 8);
			}else
				holder.txtContent.setVisibility(View.GONE);
			
			holder.image.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(mContext, ImageViewActivity.class);
					i.putExtra("Link", detail.Image);
					mContext.startActivity(i);
				}
			});
			
			return row;
//		}
	}
	
}
